package com.apesay.service.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apesay.dao.MongoDAO;
import com.apesay.exception.ServiceException;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class APIService {

	@Autowired
	private MongoDAO mongoDao;

	/**
	 * 
	 * get(按id获取对象)
	 * 
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception
	 *             String
	 * @exception
	 * @since 1.0.0
	 */
	public JSONObject get(String model, String id){
		String entity = mongoDao.get(model, id);
		if(entity!=null){
			JSONObject jsonObj = JSON.parseObject(entity);
			jsonObj.remove("_id");
			return jsonObj;
		}
		return null;
	}

	/**
	 * 
	 * save(保存对象)
	 * 
	 * @param model
	 * @param json
	 * @return JSONObject
	 * @ 
	 * @throws Exception
	 *             String
	 * @exception
	 * @since 1.0.0
	 */
	public JSONObject save(String model, String json)  {
		JSONObject returnVaue = mongoDao.save(model, json);
		return returnVaue;
	}

	/**
	 * 重命名
	 * renameCollection:
	 *
	 * @author sid
	 * @param model
	 * @param newName
	 * @return
	 */
	public JSONObject renameCollection(String model, String newName){
		JSONObject returnVaue = mongoDao.renameCollection(model, newName);
		return returnVaue;
	}

	/**
	 * 
	 * update(更新某对象)
	 * 
	 * @param model
	 * @param json
	 * @param id
	 * @return
	 * @throws Exception
	 *             String
	 * @exception
	 * @since 1.0.0
	 */
	public JSONObject update(String model, String json, String id){
		return mongoDao.update(model, json, id);
	}

	/**
	 * 
	 * delete(删除某对象)
	 * 
	 * @param model
	 * @param id
	 * @return JSONObject
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public JSONObject delete(String model, String id){
		return mongoDao.delete(model, id);
	}

	/**
	 * deleteByField:
	 *
	 * @author sid
	 * @param modelTemp
	 */
	public void deleteByField(String modelTemp, DBObject delObj) {
		mongoDao.delete(modelTemp, delObj);
	}

	/**
	 * 删除表
	 * dropTable:
	 *
	 * @author sid
	 * @param table
	 */
	public void dropTable(String table) {
		mongoDao.dropTable(table);
	}

	/**
	 * 获取自增id
	 * getModelInc:
	 *
	 * @author sid
	 * @param table
	 * @ e
	 */
	public int getModelInc(String table)  {
		return mongoDao.getModelInc(table);
	}

}
